package com.bai.Service.Impl;

import com.bai.Constant.FileUploadConst;
import com.bai.Controller.FileController;
import com.bai.Mapper.FileMapper;
import com.bai.Pojo.SysFile;
import com.bai.Service.FileService;
import com.bai.Utils.DtoResultUtil;
import com.bai.Utils.MyFileUtil;
import com.bai.dto.Children;
import com.bai.dto.PublicResponseBody;
import com.bai.dto.TreeDto;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FileServiceImpl implements FileService {
    public void setFileMapper(FileMapper fileMapper) {
        this.fileMapper = fileMapper;
    }

    private FileMapper fileMapper;
    @Override
    public PublicResponseBody addFile(String path,  MultipartFile file) throws IOException {
        //添加到系统
        String savePath = MyFileUtil.saveFile(path, file);
        if (savePath!=null){
            //添加到数据库
            int parentId = fileMapper.getParentId(path.substring(FileController.getRootPath().length()));
            if (parentId>0){
                SysFile sysFile=new SysFile(file.getOriginalFilename(),savePath.substring(FileController.getRootPath().length()-7), Long.toString(file.getSize()), 0, parentId);
                long id = fileMapper.addFile(sysFile);
                Map<String,String> data=new HashMap();
                data.put("src",savePath.substring(FileController.getRootPath().length()));
                data.put("id",String.valueOf(id));
                return DtoResultUtil.resultSuccess(data);
            }
        }
        return DtoResultUtil.result(FileUploadConst.error_path,"error_path");
    }
    @Override
    public PublicResponseBody makeDir(String name, String path) {
        //添加到系统
        if (MyFileUtil.makeFolder(path,name)){
            //添加到数据库
            String substring = path.substring(FileController.getRootPath().length());
            int parentId = fileMapper.getParentId(substring);
            if (parentId>0){
                SysFile sysFile=new SysFile(name, path.substring(FileController.getRootPath().length() - 7) + name, "0", 1, parentId);
                long id = fileMapper.addFile(sysFile);
                Map<String,String> data=new HashMap();
                data.put("src",path.substring(FileController.getRootPath().length() - 7) + name);
                data.put("id",String.valueOf(id));
                return DtoResultUtil.resultSuccess(data);
            }
        }
        return DtoResultUtil.result(FileUploadConst.error_path,"error_path");
    }
    @Override
    public PublicResponseBody delFile(int fileId, String path) {
        int i = fileMapper.delFile(fileId);
        if (i>0){
            boolean delete = new File(path).delete();
            if (delete){
                return DtoResultUtil.resultSuccess();
            }
            return DtoResultUtil.result(FileUploadConst.error_non_existent,"error_non_existent");
        }else if (i==0){
            return DtoResultUtil.result(FileUploadConst.error_non_existent,"error_non_existent");
        }else {
            return DtoResultUtil.resultError();
        }
    }
    @Override
    public TreeDto getFileList(int parentId) {
        Map<String,String> status=new HashMap();
        List<SysFile> fileList = fileMapper.getFileList(parentId);
        if (fileList!=null){
            List<Children> data=new ArrayList<>();
            for (SysFile sysFile : fileList) {
                data.add(new Children(sysFile));
            }
            status.put("code","200");
            status.put("message","操作成功");
            return new TreeDto(status,data);
        }
        status.put("code","500");
        status.put("message","操作操作失败");
        return new TreeDto(status);
    }
    @Override
    public PublicResponseBody getFileCount() {
        Map<String,Integer> data=new HashMap<String, Integer>();
        data.put("fileCount",fileMapper.getFileCount(0));
        data.put("dirCount",fileMapper.getFileCount(1));
        return DtoResultUtil.resultSuccess(data);
    }


}
